#!/bin/bash
# Ubuntu 路由器自动安装脚本

set -e

echo "=========================================="
echo "Ubuntu 路由器自动安装脚本"
echo "=========================================="

# 检查 root 权限
if [ "$EUID" -ne 0 ]; then 
    echo "错误: 需要 root 权限运行此脚本"
    echo "请使用: sudo bash install.sh"
    exit 1
fi

# 检测操作系统
if [ ! -f /etc/os-release ]; then
    echo "错误: 无法检测操作系统"
    exit 1
fi

source /etc/os-release
if [[ "$ID" != "ubuntu" ]]; then
    echo "警告: 此脚本主要针对 Ubuntu 系统设计"
    read -p "是否继续? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

echo "更新软件包列表..."
apt-get update

echo "安装必要的软件包..."
apt-get install -y \
    python3-yaml \
    netplan.io \
    bridge-utils \
    dnsmasq \
    hostapd \
    iw \
    iptables-persistent \
    avahi-daemon \
    netfilter-persistent

echo "安装 Python 依赖..."
pip3 install pyyaml --break-system-packages || pip3 install pyyaml

echo "设置脚本权限..."
chmod +x router_setup.py

echo "=========================================="
echo "安装完成！"
echo "=========================================="
echo ""
echo "下一步操作:"
echo "1. 复制配置文件模板: cp config.yaml.example config.yaml"
echo "2. 编辑配置文件: nano config.yaml"
echo "3. 根据实际情况修改接口名称和配置"
echo "4. 运行配置脚本: sudo python3 router_setup.py"
echo ""

